package com.situ.stl.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.situ.stl.constants.Constants;
import com.situ.stl.entity.Coach;
import com.situ.stl.entity.Course;
import com.situ.stl.entity.Member;
import com.situ.stl.entity.Reserve;
import com.situ.stl.mapper.MemberMapper;
import com.situ.stl.mapper.ReserveMapper;
import com.situ.stl.service.MemberService;
import com.situ.stl.vo.MemberVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;
@Service
public class MemberServiceImpl implements MemberService {
    @Autowired
    private MemberMapper memberMapper;
    @Autowired
    private ReserveMapper reserveMapper;
    @Override
    public List<Member> select(Member member) {
        return memberMapper.select(member);
    }

    @Override
    public PageInfo searchByPage(Integer page, Integer limit, Member member) { if (ObjectUtils.isEmpty(page)) page = 1;
        if (ObjectUtils.isEmpty(limit)) limit = 10;
        // 开启分页查询
        PageHelper.startPage(page, limit);
        List<Member> members = memberMapper.select(member);
        PageInfo<Member> pageInfo = new PageInfo<>(members);
        return pageInfo;
    }

    @Override
    public int modifyMember(Member member) throws Exception {
        //查看手机号是否重复
        Member dbMember = memberMapper.selectMemberByPhone(member.getPhone());
        if (!ObjectUtils.isEmpty(dbMember) && member.getId() != dbMember.getId()){
            throw new Exception("手机号被占用");
        }
        String md5Password = getMd5Password(member.getPassword(), member.getPhone());
        if (!md5Password.equalsIgnoreCase(dbMember.getPassword())){
            return Constants.PASSWORD_ERROR;
        }
        //密码加密
        if (StringUtils.hasText(member.getNewPassword())){
            String md5Password1 = getMd5Password(member.getNewPassword(),member.getPhone());
            member.setPassword(md5Password1);
        }
        member.setBalance(dbMember.getBalance()+member.getBalance());
        return memberMapper.modifyMember(member);
    }

    @Override
    public int addMember(Member member) throws Exception {
        //查看手机号是否重复
        Member dbMember = memberMapper.selectMemberByPhone(member.getPhone());
        if (!ObjectUtils.isEmpty(dbMember)){
            throw new Exception("手机号被占用");
        }
        //密码加密
        String md5Password = getMd5Password(member.getPassword(), member.getPhone());
        member.setPassword(md5Password);
        member.setCreateTime(new Date());
        return memberMapper.insert(member);
    }

    @Override
    public int deleteMember(Integer id) throws Exception {
        Member member = memberMapper.selectMemberById(id);
        if (ObjectUtils.isEmpty(member)){
            throw  new Exception("会员不存在！");
        }
        List<Reserve> reserves = reserveMapper.selectByMemberId(id);
        if (!reserves.isEmpty()) {
            throw new Exception("该会员还有课程，无法删除！");
        }
        return memberMapper.delete(id);

    }
    @Override
    public Member getByPhone(String phone) {
        return memberMapper.selectMemberByPhone(phone);
    }

    //使用md5对密码进行盐值加密
    private  String getMd5Password(String password, String salt) {
        //三次加密
        for (int i = 0; i < 3; i++) {
            String newPs = salt + password + salt;
            //md5的16进制方式加密
            password = DigestUtils.md5DigestAsHex(newPs.getBytes()).toUpperCase();
        }
        return password;
    }
    @Override
    public int resetPwd(MemberVo member)throws Exception {
        return 0;
    }
    @Override
    public Member login(MemberVo member) throws Exception {
        Member loginMember = memberMapper.selectMemberByPhone(member.getPhone());
        if (ObjectUtils.isEmpty(loginMember)){
            throw new Exception("手机号不存在！");
        }

        String md5Password = getMd5Password(member.getPassword(), member.getPhone());
        if (!md5Password.equals(loginMember.getPassword())){
            throw new Exception("密码不正确");
        }

        return loginMember;
    }

}
